/*
 * $Id: FileLineCounter.groovy 5 2008-12-10 03:04:30Z marcos.silva $
 *
 * Copyright 2008 Marcos Silva Pereira
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */ 
package com.google.code.grailssloc

/**
 * @author Marcos Silva Pereira
 */
public class FileLineCounter {
	 
	def singleLineComment(line) {
		line?.trim().startsWith('//')
	}
	
	def multiLineComment(line) {
		line?.trim().contains('/*')
	}
	
	def endMultiLineComment(line) {
		line?.trim().contains('*/')
	}
	
	def blankLine(line) {
		line == null || line.trim().equals('')
	}
	
	def countLinesFor(text) {
		
		def result = new CounterResults()
		
		def insideAComment	= false
		
		text.eachLine { line ->
		
			if(blankLine(line)) {
				print true
				result.incrementBlankLines()
				return
			}
			
			if(multiLineComment(line)) { insideAComment = true }
			
			if(singleLineComment(line)) {
				result.incrementCommentLines()
				return
			}
			
			if(endMultiLineComment(line)) {
				result.incrementCommentLines()
				insideAComment = false
				return
			}
			
			if(insideAComment) { 
				result.incrementCommentLines()
				return
			}
			
			result.incrementCodeLines()
			
		}
		
		return result
	}
	 
}
